package com.ymatou.iapi.userservice.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.enums.AppSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.ChannelSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.TerminalSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.ThirdPartyTypeEnum;
import com.ymatou.iapi.userservice.parameter.enums.UserTypeEnum;
import com.ymatou.iapi.userservice.parameter.enums.VisitLanguageEnum;
import com.ymatou.iapi.userservice.parameter.req.CreateThirdPartyUserReq;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.parameter.req.ThirdPartyUserReq;
import com.ymatou.iapi.userservice.service.CreateThirdPartyUserCall;
import com.ymatou.iapi.userservice.service.CreateUserCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.ThirdpartyUserbind;
import com.ymttest.database.model.ThirdpartyUserbindHistory;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.Userinfoex;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
/**
*锁定用户接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CreateThirdPartyUser{
	private static CreateThirdPartyUserReq createThirdPartyUserReq;
	private static CreateThirdPartyUserCall createThirdPartyUserCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建第三方用户接口");
	}

	@Before
	public void caseUp() {
		createThirdPartyUserReq=new CreateThirdPartyUserReq();
		createThirdPartyUserCall=new CreateThirdPartyUserCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createThirdPartyUserCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_001() {
		Logger.start(true,"注册weixin新用户成功");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setHeadimgurl("http://wx.qlogo.cn/mmopen/uI5pczeERTY36SclJhUtdDRPN6adUrxvjIWtYyK7MtM58zHxYqcbk8hFib2mXic2rTocOlI4ib43QUx1iayZDzJxoicVR7ibiaqKqGn/0");
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid=createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			verifyDB(uid,createThirdPartyUserReq);
//			//TODO 使用java登录验证注册成功
//			Logger.comment("开始登陆,验证用户被锁定");
//			LoginAuthBean lab = new LoginAuthBean();
//			LoginAuthCall lac = new LoginAuthCall();
//			lab.setLoginId(CreateUserBean.getLoginId());
//			lab.setPas("123456a");
//			lab.setSourceType("app");
//			lab.setClientIp(CreateUserBean.getClientIp());
//			lac.setData(false,lab);
//			lac.callService();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_002() {
		Logger.start(true,"注册weibo新用户成功");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.Weibo);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.Weibo);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid=createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			verifyDB(uid,createThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_003() {
		Logger.start(true,"注册Alipay新用户成功");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.Alipay);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.Alipay);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid=createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			verifyDB(uid,createThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_004() {
		Logger.start(false,"ThirdPartyType为空,失败");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(null);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("false", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("ThirdPartyType 未提供", createThirdPartyUserCall.getString("LastErrorMessage"), "验证错误信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_005() {
		Logger.start(false,"ThirdPartyId为空,失败");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyId(null);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("false", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("ThirdPartyId 未提供", createThirdPartyUserCall.getString("LastErrorMessage"), "验证错误信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_006() {
		Logger.start(false,"RegSource为空,失败");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setAppSource(null);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("false", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("RegSource 未提供", createThirdPartyUserCall.getString("LastErrorMessage"), "验证错误信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	public void Tc_CreateThirdPartyUser_007() {
		//为了兼容老接口放开对ClientIp的限制
		Logger.start(false,"ClientIp为空,失败");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setClientIp(null);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("false", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("ClientIp 未提供", createThirdPartyUserCall.getString("LastErrorMessage"), "验证错误信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_008() {
		Logger.start(false,"重复注册,失败");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("false", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100007", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("已被使用", createThirdPartyUserCall.getString("LastErrorMessage"), "验证LastErrorMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_009() {
		Logger.start(true,"注册Alipay新用户附带ChannelSource成功JIRACONN-11245");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.Alipay);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.Alipay);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid=createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			verifyDB(uid,createThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_010() {
		Logger.start(true,"注册QQ新用户附带ChannelSource成功JIRACONN-11245");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.QQ);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.QQ);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid=createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			verifyDB(uid,createThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_011() {
		Logger.start(true,"注册Weibo新用户附带ChannelSource成功JIRACONN-11245");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.Weibo);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.Weibo);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid=createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			verifyDB(uid,createThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_012() {
		Logger.start(true,"注册WeiXin新用户附带ChannelSource成功JIRACONN-11245");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.WeiXin);
			createThirdPartyUserReq.setHeadimgurl("http://wx.qlogo.cn/mmopen/uI5pczeERTY36SclJhUtdDRPN6adUrxvjIWtYyK7MtM58zHxYqcbk8hFib2mXic2rTocOlI4ib43QUx1iayZDzJxoicVR7ibiaqKqGn/0");
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid=createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			verifyDB(uid,createThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CreateThirdPartyUser_013() {
		Logger.start(true,"注册Other(官方渠道)新用户附带ChannelSource成功JIRACONN-11245");
		try {
			createThirdPartyUserReq=JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.QQ);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.Other);
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid=createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			verifyDB(uid,createThirdPartyUserReq);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyDB(Integer uid, ThirdPartyUserReq cq) {
		JUserWapper ju=new JUserWapper();
		Logger.comment("验证Userinfo");
		List<Userinfo> lui = ju.selectUserinfoByUid(uid);
		Logger.verifyEquals(1, lui.size(), "验证记录条数");
		Userinfo ui = lui.get(0);
		Logger.verifyEquals(cq.getThirdPartyUserName(), ui.getLoginId(), "验证LoginId");
		Logger.verifyEquals(cq.getMobile(), ui.getLoginMobile(), "验证LoginMobile");
		Logger.verifyEquals(cq.getMail(), ui.getLoginMail(), "验证LoginMail");
//		Logger.verifyEquals(cq.getUserType().getCode(), ui.getUserType(), "验证UserType");
		Logger.verifyEquals(0, ui.getUserState(), "验证UserState");
		
		Logger.comment("验证Userinfoex");
		List<Userinfoex> luiex = ju.selectUserinfoexByUid(uid);
		Logger.verifyEquals(1, luiex.size(), "验证记录条数");
		Userinfoex uiex = luiex.get(0);
//		Logger.verifyEquals(cq.getChannelSource().getCode(), uiex.getChannelSource(), "验证ChannelSource");
//		Logger.verifyEquals(cq.getRegSource().getCode(), uiex.getRegSource(), "验证RegSource");
		if(cq.getTerminalSource()!=null){
			Logger.verifyEquals(cq.getTerminalSource().getCode(), uiex.getTerminalSource(), "验证TerminalSource");
		}
		if(cq.getChannelSource()!=null){
			Logger.verifyEquals(cq.getChannelSource().getCode(), uiex.getChannelSource(), "验证ChannelSource");
		}
		Logger.verifyEquals(cq.getEquimentid(), uiex.getEquimentid(), "验证Equimentid");
		Logger.verifyEquals(cq.getDeviceId(), uiex.getDeviceId(), "验证DeviceId");
		Logger.verifyEquals(cq.getClientIp(), uiex.getClientIp(), "验证ClientIp");
		if(cq.getThirdPartyType().equals(ThirdPartyTypeEnum.WeiXin)){
			Logger.verifyEquals(cq.getHeadimgurl().substring(0,cq.getHeadimgurl().length()-1)+132, uiex.getLogoUrl(), "验证LogoUrl");
		}
		
		//TODO 验证thirdparty_userbind thirdparty_userbind_history
		Logger.comment("验证thirdparty_userbind");
		ThirdpartyUserbind thirdinfo = ju.selectthirdpartyuserbind(uid).get(0);
		Logger.verifyEquals(cq.getThirdPartyId(), thirdinfo.getThirdPartyId(), "验证ThirdPartyId");
		Logger.verifyEquals(cq.getThirdPartyType().getCode(), thirdinfo.getThirdPartyType(), "验证ThirdPartyType");
		Logger.verifyEquals(cq.getOpenId(), thirdinfo.getOpenId(), "验证OpenId");
		
		Logger.comment("验证thirdparty_userbind_history");
		ThirdpartyUserbindHistory thirdinfohist = ju.selectthirdpartyuserbindhistory(uid).get(0);
		Logger.verifyEquals(true, thirdinfohist.getBinding(), "验证binding状态");
		Logger.verifyEquals(cq.getCity(), thirdinfohist.getCity(), "验证city");
		Logger.verifyEquals(cq.getCountry(), thirdinfohist.getCountry(), "验证Country");
		Logger.verifyEquals(cq.getProvince(), thirdinfohist.getProvince(), "验证Province");
		Logger.verifyEquals(cq.getAppSource().getCode(), thirdinfohist.getAppSource(), "验证AppSource");
		Logger.verifyEquals(cq.getClientIp(), thirdinfohist.getClientIp(), "验证ClientIp");
		Logger.verifyEquals(cq.getDeviceId(), thirdinfohist.getDeviceId(), "验证DeviceId");
		Logger.verifyEquals(cq.getMail(), thirdinfohist.getEmail(), "验证Email");
		Logger.verifyEquals(cq.getEquimentid(), thirdinfohist.getEquimentid(), "验证Equimentid");
		Logger.verifyEquals(cq.getGmtDecay(), thirdinfohist.getGmtDecay(), "验证GmtDecay");
		if(cq.getThirdPartyType().equals(ThirdPartyTypeEnum.WeiXin)){
			///132
			Logger.verifyEquals(cq.getHeadimgurl().substring(0,cq.getHeadimgurl().length()-1)+132, uiex.getLogoUrl(), "验证LogoUrl");
			Logger.verifyEquals(cq.getHeadimgurl().substring(0,cq.getHeadimgurl().length()-1)+132, thirdinfohist.getHeadimgurl(), "验证Headimgurl");
		}else{
			Logger.verifyEquals(cq.getHeadimgurl(), thirdinfohist.getHeadimgurl(), "验证Headimgurl");
		}
		Logger.verifyEquals(cq.getMobile(), thirdinfohist.getLoginMobile(), "验证LoginMobile");
		Logger.verifyEquals(cq.getOpenId(), thirdinfohist.getOpenId(), "验证OpenId");
		Logger.verifyEquals(cq.getSecret(), thirdinfohist.getSecret(), "验证Secret");
		Logger.verifyEquals(cq.getTerminalSource().getCode(), thirdinfohist.getTerminalSource(), "验证TerminalSource");
		Logger.verifyEquals(cq.getThirdParty(), thirdinfohist.getThirdParty(), "验证ThirdParty");
		Logger.verifyEquals(cq.getThirdPartyId(), thirdinfohist.getThirdPartyId(), "验证ThirdPartyId");
		Logger.verifyEquals(cq.getThirdPartyToken(), thirdinfohist.getThirdPartyToken(), "验证ThirdPartyToken");
		Logger.verifyEquals(cq.getThirdPartyType().getCode(), thirdinfohist.getThirdPartyType(), "验证ThirdPartyType");
		Logger.verifyEquals(cq.getThirdPartyUserName(), thirdinfohist.getThirdPartyUserName(), "验证getThirdPartyUserName");
		Logger.verifyEquals(cq.getUserGender().getCode(), thirdinfohist.getUserGender(), "验证UserGender");
		Logger.verifyEquals(cq.getGrade(), thirdinfohist.getUserGrade(), "验证UserGrade");
		Logger.verifyEquals(cq.getGradeType(), thirdinfohist.getUserGradeType(), "验证UserGradeType");
	}
}